﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Lhxzn.LangExtractor.IniFile;


#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：12/20/2024 3:42:44 PM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion

namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:LanguageFile
    /// 描    述:
    /// 修改时间:12/20/2024 3:42:44 PM
    /// </summary>
    public class LanguageFile:IniFile
    {
        #region 成员变量

        #region private



        #endregion


        #region protected



        #endregion


        #region public

        public const string FileExtension = ".lng";



        /// <summary>
        /// 所属于的语言包
        /// </summary>
        public LanguagePackage Package { get; internal set; }

        public string Name => Path.GetFileNameWithoutExtension(this.FilePath);



        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:2022/4/1 10:36:30
        /// </summary>
        public LanguageFile(string filePath) : base(filePath)
        {
            this.IsLogNonexistent = true;
        }

        #endregion

        #region 父类函数重载、接口实现


        /// <summary>
        /// 读取一个值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="Key"></param>
        /// <param name="strDefault"></param>
        /// <returns></returns>
        public override string ReadValue(string section, string Key, string strDefault)
        {
            if (Key == null)
            {
                return strDefault != null ? strDefault : string.Empty;
            }
            string res = Key;
            if (ContainsKey(section, Key))
            {
                res = m_dictIniCache[section][Key];
            }
            else
            {
                if (!string.IsNullOrEmpty(strDefault))
                {
                    res = strDefault;
                }
                if (IsLogNonexistent)
                {
                    WriteValue(section, Key, res);
                }

            }
            return res;
        }

        /// <summary>
        /// 读取键值对应的内容并且以format替换对象
        /// </summary>
        /// <param name="section"></param>
        /// <param name="Key"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual string ReadKeyFormat(string section, string Key, object[] args)
        {
            string rlt = this.ReadValue(section, Key);
            return string.Format(rlt, args);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="section"></param>
        /// <param name="Key"></param>
        /// <param name="Default"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual string ReadKeyFormat(string section, string Key, string Default, object[] args)
        {
            string rlt = ReadValue(section, Key, Default);
            return string.Format(rlt, args);
        }

        #endregion

        #region 函数

        #region private



        #endregion


        #region protected



        #endregion


        #region public



        /// <summary>
        /// 根据‘_’分割获取名称
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public string GetStringByNameSplit(string Name, string section)
        {
            string key = GetNameBySplit(Name);

            return ReadValue(section, key);
        }

        /// <summary>
        /// 根据‘_’分割获取名称
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="section"></param>
        /// <param name="defaultText"></param>
        /// <returns></returns>
        public string GetStringByNameSplit(string Name, string section, string defaultText)
        {
            string key = GetNameBySplit(Name);

            return ReadValue(section, key, defaultText);
        }

        /// <summary>
        /// 根据第一个下划线确定名称，名称为第一个下划线后的字符串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetNameBySplit(string name)
        {
            int index = name.IndexOf('_');
            if (index < 0)
                return name;
            else
            {
                return name.Substring(index + 1);
            }

        }

        public Dictionary<string, Dictionary<string, KeyData>> GetIniCache()
        {
            return m_dictIniCache;
        }

        #endregion

        #endregion
    }
}
